import GetSystemInfoResult = UniApp.GetSystemInfoResult
import GetProviderRes = UniApp.GetProviderRes
import LoginRes = UniApp.LoginRes
import {Provider} from '@/uni/const/ProviderType'
import GetUserInfoRes = UniApp.GetUserInfoRes
import {systemModule} from '@/plugins/store'
import Toast from "@/utils/Toast";
import Alert from "@/utils/Alert";
import AppMsg from "@/const/AppMsg";
import ProviderMsg from "@/const/ProviderMsg";

export default class UniUtil {
  public static textCopy(copyText: string, hint?: string) {
    return new Promise((resolve, reject) => {
      uni.setClipboardData({
        data: copyText,
        success() {
          if (hint) {
            uni.hideToast()
            Toast.toast(hint)
          }
          resolve(null)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  }

  public static createRewardedVideoAd(adUnitId: string) {
    if (systemModule.isMp) {
      return uni.createRewardedVideoAd({
        adUnitId: adUnitId
      })
    } else {
      return uni.createRewardedVideoAd({
        //eslint-disable-next-line
        //@ts-ignore
        adpid: adUnitId
      })
    }
  }

  static copyLink(webUrl: string) {
    return UniUtil.textCopy(webUrl, '链接已复制，可在浏览器打开')
  }

  public static upxToPx(rpx: number): number {
    return uni.upx2px(rpx)
  }

  public static getSystemInfo(): Promise<GetSystemInfoResult> {
    return new Promise<any>(resolve =>
      uni.getSystemInfo({
        success: (res) => {
          resolve(res)
        }
      })
    )
  }

  public static getProvider(): Promise<GetProviderRes> {
    return new Promise<any>(resolve =>
      uni.getProvider({
        service: 'oauth',
        success: (res) => {
          resolve(res)
        }
      })
    )
  }

  public static showLoading(loadText: string) {
    uni.showLoading({title: loadText || ''})
  }

  public static hideLoading() {
    uni.hideLoading()
  }

  public static actionSheet(itemList: string[]): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      uni.showActionSheet({
        itemList: itemList,
        success(res) {
          resolve(res.tapIndex)
        },
        fail(res) {
          reject(res)
        }
      })
    })
  }

  public static compressImage(filePath: string, quality: number): Promise<string> {
    return new Promise<string>((resolve, reject) => {
      uni.compressImage({
        src: filePath,
        //默认最低20
        quality: Math.max(quality, 20),
        success: res => {
          resolve(res.tempFilePath)
        },
        fail: err => {
          reject(err)
        }
      })
    })
  }

  public static install(filePath: '_www/' | '_doc/' | '_documents/' | '_downloads/') {
    return new Promise<any>((resolve, reject) => {
      plus.runtime.install(filePath, {
        force: false
      }, () => {
        resolve(null)
      }, (e) => {
        reject(e)
      })
    })
  }

  static showShareMenu() {
    // #ifdef MP-QQ || MP-WEIXIN
    uni.showShareMenu({})
    // #endif
  }

  /*在回调中调用 wx.login 登录，可能会刷新登录态。此时服务器使用 code 换取的 sessionKey
       不是加密时使用的 sessionKey，导致解密失败。建议开发者提前进行 login；
       或者在回调中先使用 checkSession 进行登录态检查，避免 login 刷新登录态。*/
  //先checksession，如果已过期，则调用登陆，
  static checkSession (): Promise<any> {
    return new Promise<any>((resolve, reject) =>
      uni.checkSession({
        success (e) {
          console.log(e)
          resolve(null)
        },
        fail (err) {
          reject(err)
        }
      })
    )
  }

  static login(provider: Provider) {
    return new Promise<LoginRes>(resolve => {
      uni.login({
        provider: provider,
        success(loginRes) {
          resolve(loginRes)
        }
      })
    })
  }

  public static getUserInfo(provider: Provider) {
    return new Promise<GetUserInfoRes>((resolve, reject) => {
      uni.getUserInfo({
        provider: provider,
        success(userInfo) {
          //登陆成功后可以获取用户信息
          //如果为登陆成功
          if (!userInfo || userInfo.errMsg !== ProviderMsg.loginSuccess || !userInfo.userInfo) {
            reject(Error('获取信息失败，请重试'))
          }
          resolve(userInfo)
        },
        fail(res) {
          reject(res)
        }
      })
    })
  }
}
